<%=

director = {
  'endpoint' => "https://#{p('director.address')}:#{p('director.port')}",
  'user' => p('hm.director_account.user'),
  'password' => p('hm.director_account.password'),
  # default to empty string so it works in micro bosh
  'client_id' => p('hm.director_account.client_id', ''),
  'client_secret' => p('hm.director_account.client_secret', ''),
  'ca_cert' => '/var/vcap/jobs/health_monitor/config/uaa.pem',
}

params = {
  'http' => {
    'port' => p('hm.http.port'),
  },
  'mbus' => {
    'endpoint' => "nats://#{p('nats.address')}:#{p('nats.port')}",
    'server_ca_path' => '/var/vcap/jobs/health_monitor/config/nats_server_ca.pem',
    'client_certificate_path' => '/var/vcap/jobs/health_monitor/config/nats_client_certificate.pem',
    'client_private_key_path' => '/var/vcap/jobs/health_monitor/config/nats_client_private_key'
  },
  'director' => director,
  'intervals' => {
    'prune_events' => p('hm.intervals.prune_events'),
    'poll_director' => p('hm.intervals.poll_director'),
    'poll_grace_period' => p('hm.intervals.poll_grace_period'),
    'log_stats' => p('hm.intervals.log_stats'),
    'analyze_agents' => p('hm.intervals.analyze_agents'),
    'agent_timeout' => p('hm.intervals.agent_timeout'),
    'rogue_agent_alert' => p('hm.intervals.rogue_agent_alert'),
    'analyze_instances' => p('hm.intervals.analyze_instances'),
  },
  'logfile' => '/var/vcap/sys/log/health_monitor/health_monitor.log',
  'loglevel' => p('hm.loglevel'),
  'plugins' => [
    {
      'name' => 'logger',
      'events' => [
        'alert',
      ],
    },
    {
      'name' => 'event_logger',
      'events' => [
        'alert',
      ],
      'options' => {
      'director' => director
      },
    },
    {
      'name' => 'json',
      'events' => [
        'alert',
        'heartbeat',
      ],
      'options' => {},
    }
  ]
}

if p('hm.email_notifications')
  smtp_plugin = {
    'name' => 'email',
    'events' => [
      'alert',
    ],
    'options' => {
      'recipients' => p('hm.email_recipients').to_a,
      'smtp' => {
        'from' => p('hm.smtp.from'),
        'host' => p('hm.smtp.host'),
        'port' => p('hm.smtp.port'),
        'domain' => p('hm.smtp.domain'),
      },
    },
  }

  if_p('hm.smtp.tls') do |tls|
    smtp_plugin['options']['smtp']['tls'] = tls
  end

  if_p('hm.smtp.auth', 'hm.smtp.user', 'hm.smtp.password') do |auth, user, password|
    smtp_plugin['options']['smtp']['auth'] = auth
    smtp_plugin['options']['smtp']['user'] = user
    smtp_plugin['options']['smtp']['password'] = password
  end

  if_p('hm.email_interval') do |email_interval|
    smtp_plugin['options']['interval'] = email_interval
  end

  params['plugins'] << smtp_plugin
end

if p('hm.tsdb_enabled')
  params['plugins'] << {
    'name' => 'tsdb',
    'events' => [
      'alert',
      'heartbeat',
    ],
    'options' => {
      'host' => p('hm.tsdb.address'),
      'port' => p('hm.tsdb.port'),
      'max_retries' => p('hm.tsdb.max_retries'),
    },
  }
end

if p('hm.resurrector_enabled')
  params['plugins'] << {
    'name' => 'resurrector',
    'events' => [
      'alert'
    ],
    'options' => {
      'director' => director,
      'minimum_down_jobs' => p('hm.resurrector.minimum_down_jobs'),
      'percent_threshold' => p('hm.resurrector.percent_threshold'),
      'time_threshold' => p('hm.resurrector.time_threshold'),
    },
  }
end

if p('hm.pagerduty_enabled')
  pagerduty_plugin = {
    'name' => 'pagerduty',
    'events' => [
      'alert',
    ],
    'options' => {
      'service_key' => p('hm.pagerduty.service_key'),
    },
  }

  if_p('hm.pagerduty.http_proxy') do |http_proxy|
    pagerduty_plugin['options']['http_proxy'] = http_proxy
  end

  params['plugins'] << pagerduty_plugin
end

if p('hm.riemann_enabled')
  riemann_plugin = {
    'name' => 'riemann',
    'events' => [
      'alert',
      'heartbeat',
    ],
    'options' => {
      'host' => p('hm.riemann.host'),
      'port' => p('hm.riemann.port'),
    },
  }

  params['plugins'] << riemann_plugin
end

if p('hm.datadog_enabled')
  datadog_plugin = {
    'name' => 'data_dog',
    'events' => [
      'alert',
      'heartbeat',
    ],
    'options' => {
      'api_key' => p('hm.datadog.api_key'),
      'application_key' => p('hm.datadog.application_key'),
    }
  }

  if_p('hm.datadog.pagerduty_service_name') do |pagerduty_service_name|
    datadog_plugin['options']['pagerduty_service_name'] = pagerduty_service_name
  end

  if_p('hm.datadog.custom_tags') do |custom_tags|
    datadog_plugin['options']['custom_tags'] = custom_tags
  end

  params['plugins'] << datadog_plugin
end

if p('hm.graphite_enabled')
  graphite_plugin = {
    'name' => 'graphite',
    'events' => [
      'heartbeat',
    ],
    'options' => {
      'host' => p('hm.graphite.address'),
      'port' => p('hm.graphite.port'),
      'max_retries' => p('hm.graphite.max_retries'),
    },
  }

  if_p('hm.graphite.prefix') do |prefix|
    graphite_plugin['options']['prefix'] = prefix
  end

  params['plugins'] << graphite_plugin
end

if_p('hm.syslog_event_forwarder_enabled') do |_|
  raise 'property hm.syslog_event_forwarder_enabled has been removed. Please co-locate the syslog-release instead to forward your logs.'
end

if p('hm.consul_event_forwarder_enabled')
  consul_event_forwarder_plugin = {
    'name' => 'consul_event_forwarder',
    'events' => [
      'alert',
      'heartbeat',
    ],
    'options' => {
      'host' => p('hm.consul_event_forwarder.host'),
      'port' => p('hm.consul_event_forwarder.port'),
      'protocol' => p('hm.consul_event_forwarder.protocol'),
      'ttl_note' => p('hm.consul_event_forwarder.ttl_note'),
      'events' => p('hm.consul_event_forwarder.events'),
      'heartbeats_as_alerts' => p('hm.consul_event_forwarder.heartbeats_as_alerts'),
    },
  }

  if_p('hm.consul_event_forwarder.namespace') do |namespace|
    consul_event_forwarder_plugin['options']['namespace'] = namespace
  end

  if_p('hm.consul_event_forwarder.params') do |params|
    consul_event_forwarder_plugin['options']['params'] = params
  end

  if_p('hm.consul_event_forwarder.ttl') do |ttl|
    consul_event_forwarder_plugin['options']['ttl'] = ttl
  end

  params['plugins'] << consul_event_forwarder_plugin
end

JSON.dump(params)

%>
